package app.nbdevices.utils;
/**
 *
 * @author zhouchao
 *
 * 用来进行存放设备下行命令的的记录，待命令执行成功之后会清除，也会定期进行数据清除
 *
 */

import lombok.Data;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

public class StackOperator {
    private static class Info{
        private String devCode;
        private String userName;
        private Integer devId;
        private Integer userId;
        private long timestramp=System.currentTimeMillis();
        public Info() {
            this.devCode = "";
            this.userName = "";
            this.devId = -1;
            this.userId = -1;
        }
        public Info(String devCode, String userName, Integer devId, Integer userId) {
            this.devCode = devCode;
            this.userName = userName;
            this.devId = devId;
            this.userId = userId;
        }
        public String getDevCode() {
            return devCode;
        }
        public void setDevCode(String devCode) {
            this.devCode = devCode;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public Integer getDevId() {
            return devId;
        }
        public void setDevId(Integer devId) {
            this.devId = devId;
        }
        public Integer getUserId() {
            return userId;
        }
        public void setUserId(Integer userId) {
            this.userId = userId;
        }
        public long getTimestramp() {
            return timestramp;
        }

    }

    //门禁设备开锁命令缓存
    @Data
    public static class DoorunlockInfo{
        private String addrId;
        private String cmdData;
        private Integer userId;
        public DoorunlockInfo(String addrId, Integer userId,String cmdData){
            this.addrId=addrId;
            this.userId=userId;
            this.cmdData=cmdData;
        }
    }
    private static Map<String, app.nbdevices.utils.StackOperator.DoorunlockInfo> unlockInfos=new ConcurrentHashMap<>();

    private static Map<String, app.nbdevices.utils.StackOperator.Info> store=new ConcurrentHashMap<>();
    /**
     * 添加开锁请求
     */
    public static void addLock(String addrId,Integer userId,String cmdData){
        unlockInfos.put(addrId,new DoorunlockInfo(addrId,userId,cmdData));
    }
    /**
     * 获取用户id
     */
    public static int getDoorUserID(String addrId){
        if ( unlockInfos.get(addrId)==null) {
            return -1;
        }

        return  unlockInfos.get(addrId).getUserId();
    }
    /**
     * 获取对象
     */
    public static DoorunlockInfo getUnlockInfo(String addrId){
        if ( unlockInfos.get(addrId)==null) {
            return null;
        }

        return  unlockInfos.get(addrId);
    }
    /**
     *
     * 该方法用来添加用户的一次开门请求及其相关信息
     *
     * @param devCode 设备编码
     * @param devId 设备id
     * @param userName 用户名
     * @param userId 用户id
     *
     */
    public static void doOneRequest(String devCode,Integer devId, String userName,Integer userId){
        store.put(devCode, new app.nbdevices.utils.StackOperator.Info(devCode, userName, devId, userId));
    }
    /**
     * 该方法用来获取该设备的相关用户ID
     *
     * @param devCode 设备编码
     * @return 用户ID
     */
    public static int getUserID(String devCode){
        if (store.get(devCode)==null) {
            return -1;
        }

        return store.get(devCode).getUserId();
    }
    /**
     * 该方法用来获取该设备的ID
     *
     * @param devCode 设备编码
     * @return 设备ID
     */
    public static int getDevID(String devCode){
        if (store.get(devCode)==null) {
            return -1;
        }

        return store.get(devCode).getDevId();
    }
    /**
     * 该方法用来获取该设备的ID
     *
     * @param devCode 设备编码
     * @return 设备编码
     */
    public static String getDevCode(String devCode){
        if (store.get(devCode)==null) {
            return "";
        }

        return store.get(devCode).getDevCode();
    }
    /**
     * 该方法用来获取该设备的用户名
     *
     * @param devCode 设备编码
     * @return 用户名
     */
    public static String getUserName(String devCode){
        if (store.get(devCode)==null) {
            return "";
        }

        return store.get(devCode).getUserName();
    }
    /**
     * 该方法用来删除记录过的信息
     *
     * @param devCode 设备编码
     */
    public static void remove(String devCode) {
        store.remove(devCode);
    }

    //执行定期任务
    static{
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                System.out.println("\n###################执行一次stack删除####################\n");
                for(Map.Entry<String, app.nbdevices.utils.StackOperator.Info> entry:store.entrySet()){
                    String key = entry.getKey();
                    if(isTimeout(key)){
                        remove(key);
                    }
                }
            }
        }, 10,6*1000*60);
    }
    //判断是否超时进行删除
    private static boolean isTimeout(String devCode){
        long timestramp = store.get(devCode).getTimestramp();
        if ((System.currentTimeMillis()-timestramp-(15*60*1000))>0) {
            return true;
        }
        return false;
    }
}